package com.tanklab.signature.common.util;
import java.time.Duration;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;

public class TimeUtils {

    // Function to get the current time
    public static String getCurrentTime() {
        LocalDateTime now = LocalDateTime.now();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        return now.format(formatter);
    }

    // Function to calculate the difference between two times in seconds
    public static long timeDifferenceInSeconds(String startTime, String endTime) {
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        LocalDateTime start = LocalDateTime.parse(startTime, formatter);
        LocalDateTime end = LocalDateTime.parse(endTime, formatter);
        Duration duration = Duration.between(start, end);

        return duration.getSeconds();
    }

    //计算间隔多少天
    public static String calculateDaysBetween(String startDateTime, String endDateTime) {
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        LocalDateTime start = LocalDateTime.parse(startDateTime, formatter);
        LocalDateTime end = LocalDateTime.parse(endDateTime, formatter);

        long daysBetween = ChronoUnit.DAYS.between(start, end);
        return String.valueOf(daysBetween);
    }

    // Function to compare two times: returns 1 if first is after second, -1 if before, 0 if equal
    public static int compareTimes(String firstTime, String secondTime) {//-1则firsttime先于secondtime
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        LocalDateTime first = LocalDateTime.parse(firstTime, formatter);
        LocalDateTime second = LocalDateTime.parse(secondTime, formatter);

        return first.compareTo(second);
    }

    //几天多少秒
    public static String daysToSeconds(String days) {
        String oneDaySecond = "86400";
        return computeUtil.multiplyLargeNumbers(days, oneDaySecond);
    }

    // Main method for testing
    public static void main(String[] args) {
        String currentTime = getCurrentTime();
        System.out.println("Current time: " + currentTime);

        String time1 = "2024-10-24 12:00:00";
        String time2 = "2024-10-24 15:30:45";

        long differenceInSeconds = timeDifferenceInSeconds(time1, time2);
        System.out.println("Time difference in seconds: " + differenceInSeconds);

        int comparison = compareTimes(time1, time2);
        if (comparison > 0) {
            System.out.println("Time1 is after Time2");
        } else if (comparison < 0) {
            System.out.println("Time1 is before Time2");
        } else {
            System.out.println("Time1 is equal to Time2");
        }
    }
}

